<template>
    <div class="header">
        <div class="logo">
            <img src="@/assets/images/基础/LOGO.png" alt="" />
        </div>

        <div class="link">
            <Headerlink />
        </div>

        <div class="info">
            <div v-if="WeatherData && WeatherPm2P5" class="weahter">
                <div @click="WeahterLink" class="textDat weahtermon">
                    <i :style="{ color: '#fff' }" :class="'qi-' + WeatherData.now.icon"></i>
                    <span class="temp"> {{ WeatherData.now.temp }}°C </span>
                    <span>
                        {{ WeatherData.now.windDir }}
                    </span>
                </div>
            </div>
        </div>

        <div class="time-box">
            <div class="time">
                {{ time }}
            </div>
            <div class="date">
                <span>{{ week }}</span>
                <span>{{ date }}</span>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import Dayjs from 'dayjs'
import { getWeather, getWeatherCityId, getWeatherPm2P5 } from '@/api/connect'
import { onMounted, onUnmounted, ref } from 'vue'
import Headerlink from './headerlink/index.vue'

// 获取当前时间
const date: any = ref('')
const time: any = ref('')
const week: any = ref('')
const get_Date_Time = () => {
    let date_time = Dayjs().format('YYYY.MM.DD/HH:mm:ss').split('/')
    date.value = date_time[0]
    time.value = date_time[1]
    const day = Dayjs().day()
    const arr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五']
    week.value = arr[day]
}
let WeatherData = ref()
let WeatherPm2P5 = ref()
const getWeatherData = async () => {
    /**
     * @description: 获取城市ID
     * @param {any} 城市名
     * @return {any} 城市ID
     */
    const CityId: any = await getWeatherCityId('海口')
    /**
     * @description: 获取城市一天的天气信息
     * @param {any} 城市ID
     * @return {any}城市一天的天气信息
     */

    const CityWeather: any = await getWeather(CityId.location[0].id)
    console.log(CityWeather, 'CityWeather')

    WeatherData.value = CityWeather

    /**
     * @description: 获取pm2.5数据
     * @param {any}  城市id
     * @return {any} pm2.5数据
     */
    const CityWeather_Pm2P5 = await getWeatherPm2P5(CityId.location[0].id)
    WeatherPm2P5.value = CityWeather_Pm2P5
}
const WeahterLink = () => {
    window.open(WeatherData.value.fxLink)
}

let timer = ref()

onMounted(async () => {
    // 获取当前日期时间
    get_Date_Time()
    // 启动定时器
    timer.value = setInterval(() => {
        get_Date_Time()
    }, 1000)
    // 获取天气数据
    await getWeatherData()
})

/**
 * 清楚定时器
 */
onUnmounted(() => {
    clearInterval(timer.value)
})
</script>
<style lang="scss" scoped>
.header {
    position: relative;
    top: 0;
    width: 100%;
    display: flex;
    height: 130px;
    background: url('@/assets/images/基础/top.png') no-repeat;
    background-size: 100% 100%;
    z-index: 10;
    .logo {
        width: 20%;
        position: absolute;
        left: 30px;
        top: 15px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        cursor: pointer;
        img {
            width: 100%;
        }
    }
    .link {
        height: 54px;
        line-height: 42px;
        top: 40px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        right: 0;
    }

    .info {
        font-size: 22px;
        height: 34px;
        right: 250px;
        top: 30px;
        position: absolute;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        .weahter {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            color: #ffffff;

            > .weahtermon {
                cursor: pointer;
                position: relative;
                height: 100%;
                margin-right: 30px;

                .temp {
                    font-weight: 800;
                }

                > span {
                    display: inline-block;
                    margin-left: 10px;
                    letter-spacing: 1px;
                }

                &::after {
                    content: '';
                    position: absolute;
                    width: 2px;
                    height: 28px;
                    background: rgba(190, 190, 190, 0.856);
                    right: -20px;
                    top: 0;
                    bottom: 0;
                    margin: auto;
                }
            }

            .aqi {
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .textDat {
                display: flex;
                justify-content: center;
                align-items: center;
                margin-left: 8px;

                i:nth-of-type(1) {
                    font-size: 30px;
                    margin-right: 10px;
                    display: inline-block;
                }
            }

            .temp {
                display: flex;
                justify-content: center;
                align-items: center;
                margin-left: -8px;
            }

            .humidity {
                display: flex;
                justify-content: center;
                margin-left: -4px;
                align-items: center;
            }

            .windDir {
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .pm25 {
                display: flex;
                justify-content: space-around;
                align-items: center;
                cursor: pointer;
                margin-left: 13px;
                margin-right: 15px;
                font-weight: 800;

                > span {
                    display: inline-block;
                    margin-right: 3px;
                }
            }
        }
    }

    .time-box {
        color: #cbcbcb;
        position: absolute;
        height: 34px;
        top: 30px;
        right: 30px;
        font-family: OPPOSans;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        width: 120px;
        .time {
            font-size: 28px;
            font-family: Tencent;
        }
        .date {
            display: flex;
            justify-content: center;
            flex-direction: column;
            align-items: flex-end;
            font-size: 16px;
            font-family: Tencent;
            span:nth-child(1) {
                margin-bottom: 5px;
            }
        }
    }
}
</style>
